package panda.server.util.datamine;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import panda.client.enumerations.MagicSet;

/**
 * Scrapes images off the completed Wizards M:tG Rise of the Eldrazi Visual Spoiler
 * @author zane.tian@gmail.com
 */
public class VisualSpoilerScraper {	
	public static void main(String[] args) {
		MagicSet set = MagicSet.RISE_OF_THE_ELDRAZI;
		
		final String setName = set.getName();
		final String filename = "C:/Users/kyruru/Desktop/" + setName + ".bat";
		
		final String wgetPrefix = "C:/GnuWin32/bin/wget -O ";
		final String sitePrefix = "http://cds028.dc1.hwcdn.net/g5y9u5z3/cds/media/images/tcg/products/riseoftheeldrazi/rdfgh8rvhs/EN/";

		// http://cds028.dc1.hwcdn.net/g5y9u5z3/cds/media/images/tcg/products/riseoftheeldrazi/rdfgh8rvhs/EN/0001_MTGROE_EN_LR.jpg

		renameFiles();
		
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(filename));
			for(int i = 0; i < set.getTotalCards(); i++) {
				out.write(wgetPrefix + (i+1) + ".jpg " + sitePrefix + padWithZeroes(i + 1) + "_MTGROE_EN_LR.jpg");
				out.newLine();
			}

			out.close();
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Takes filenames like 0001_MTGROE_EN_LR.jpg@doppl=3f46e66974838aff74d38aafa5169fdfe006275b&dopsig=270722acb50b06dc4fe0434a1fe26558
	 * and renames them to [1-248].jpg
	 */
	private static void renameFiles() {
		String root = "C:/Users/kyruru/Desktop/roe";
		File dir = new File(root);
		
		String[] children = dir.list();
		for(int i = 0; i < 248; i++) {
			String child = children[i];
			System.out.println(child);
			File currentFile = new File(root + "/" + child);
			

			System.out.println(currentFile.getAbsolutePath());
			System.out.println(currentFile.renameTo(new File(root + "/" + (i+1) + ".jpg")));
		}
		
	}
	
	/* takes an integer < 1000 and pads it until it has 4 digits total. leading zeroes. */
	private static String padWithZeroes(int n) {
		if(n > 999) {
			throw new IllegalStateException();
		}
		
		String stringForm = String.valueOf(n);
		String result = stringForm;
		
		for(int i = stringForm.length(); i <= 4; i++) {
			result = "0" + result; 
		}
		
		return result;
	}
}